/*
 * Copyright (c) 2009, Hamish Morgan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the University of Sussex nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package piccat.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LayoutManager;
import javax.swing.JPanel;

/**
 * A transparent panel that sits ontop of each sides grid view. When pictures
 * are picked up from the grid, by the user, they are transfered to the glass
 * pane. Here they can be moved and possitioned exactly. If they are dropped
 * over a grid cell then the picture moves to that cell in the grid. Otherwise
 * it stay in the glass pane. This allows pictures to moved in small steps
 * rather than a single movement, which can be a problem for children.
 *
 * @author Hamish Morgan
 */
public class GlassPane extends JPanel {

    private static final int alpha = 32;
    public static final Color NO_FILL = null;
    public static final Color RED = new Color(255, 0, 0, alpha);
    public static final Color GREEN = new Color(0, 255, 0, alpha);
    public static final Color BLUE = new Color(0, 0, 255, alpha);
    public static final Color YELLOW = new Color(255, 255, 0, alpha);
    public static final Color MAGENTA = new Color(255, 0, 255, alpha);
    public static final Color CYAN = new Color(0, 255, 255, alpha);
    private Color fillColour;

    public GlassPane() {
        fillColour = NO_FILL;
        setOpaque(false);
        setLayout(new GlassLayout());
    }

    public void setFillColour(Color fillColour) {
        this.fillColour = fillColour;
    }

    public Color getFillColour() {
        return fillColour;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (fillColour != NO_FILL) {
            final Graphics2D g2 = (Graphics2D) g;
            g2.setColor(fillColour);
            g2.fill(g.getClip());
        }
    }

    @Override
    public boolean isOptimizedDrawingEnabled() {
        // no because child components can overlap
        return false;
    }

    private static class GlassLayout implements LayoutManager {

        private static final Dimension MIN_SIZE = new Dimension(640, 480);
        private static final Dimension PREF_SIZE = new Dimension(640, 480);

        public void addLayoutComponent(String name, Component comp) {
        }

        public void removeLayoutComponent(Component comp) {
        }

        public Dimension preferredLayoutSize(Container parent) {
            return PREF_SIZE;
        }

        public Dimension minimumLayoutSize(Container parent) {
            return MIN_SIZE;
        }

        public void layoutContainer(Container parent) {

            for (Component c : parent.getComponents()) {
                final int left = 0;
                final int right = parent.getWidth() - c.getWidth();
                final int top = 0;
                final int bottom = parent.getHeight() - c.getHeight();

                int newX = c.getX();
                int newY = c.getY();

                if (c.getX() < left)
                    newX = left;
                else if (c.getX() > right)
                    newX = right;
                if (c.getY() < top)
                    newY = top;
                else if (c.getY() > bottom)
                    newY = bottom;

                if (newX != c.getX() || newY != c.getY())
                    c.setLocation(newX, newY);
            }
        }
    }
}
